<?php
ini_set('display_errors', 1);
ini_set('log_errors', 1);
ini_set('error_log', dirname(__FILE__) . '/error_log.txt');
error_reporting(E_ERROR);

global $db, $cid;

$host = 'localhost';        // normally localhost, but not necessarily.                                                                                               
$usr  = ' ';        // your db userid^                                                                                                                     
$pwd  = ' ';                 // your db password^                                                                                                          
$db   = ' ';        // your database^  


$viejo = array(
  'path'=>'/home/old/public_html/',
  'url'=>'old.dmsdn.com',
);


$nuevo = array(
  'path'=>'/home/new/public_html/',
  'url'=>'new.dmsdn.com',
);


// --------------------



$cid = mysql_connect($host,$usr,$pwd);
if (!$cid) { echo("Connecting to DB Error: " . mysql_error() . "<br/>"); }
mysql_query("set names 'utf8'");


// wp_options
$sql[] = "update wp_options set option_value = replace(option_value, '{$viejo['url']}','{$nuevo['url']}'); "  ;

// wp_posts
$sql[] = "update wp_posts set guid = replace(guid, '{$viejo['url']}','{$nuevo['url']}'); " ;
$sql[] = "update wp_posts set post_content = replace(post_content, '{$viejo['url']}','{$nuevo['url']}'); "       ;

 
foreach($sql as $s){
	echo "\nEjecutando $s";
	$result = mysql_db_query($db,$s,$cid);
	if (!$result) { echo("ERROR: " . mysql_error() . "<br/>$SQL<br/>"); }

}


// wp_layerslider
replaceObject($table='wp_icl_translation_status',$id='rid',$data='translation_package');

 








	function replaceObject($table,$id='id',$data='data'){
		global $db, $cid, $viejo, $nuevo;

		$SQL = "SELECT * FROM `$table` ";

		echo "\n$SQL";

		$retid = mysql_db_query($db, $SQL, $cid);
		//if (!$retid) { echo("ERROR: " . mysql_error()); }
		while ($row = mysql_fetch_array($retid)) {
			$option_id=$row[$id];
			$option_value=$row[$data];
			echo " $option_id ... ";
			$object = unserialize($option_value);
			if($object){
				$object_new = replaceTree($viejo,$nuevo,$object);
				$option_value_serialize = serialize($object_new);
			}else{
				$option_value_serialize = replaceTree($viejo,$nuevo,$option_value);
			}
			$UPDATE_SQL = "UPDATE `$table` SET `$data` = '".mysql_real_escape_string($option_value_serialize)."' WHERE `$id` = '$option_id' ";
			//   echo $UPDATE_SQL.'<hr>';
			$result = mysql_db_query($db,$UPDATE_SQL,$cid);
			if (!$result) { echo("ERROR: " . mysql_error() . "<br/>$SQL<br/>"); }

		}

	}

	/**
	 * Recursive alternative to str_replace that supports replacing keys as well
	 *
	 * @param string  $search
	 * @param string  $replace
	 * @param array   $array
	 * @param boolean $keys_too
	 *
	 * @return array
	 */
	function replaceTree($search="", $replace="", $array=false, $keys_too=false)
	{
		if (!is_array($array)) {
			// Regular replace
			return str_replace($search, $replace, $array);
		}

		$newArr = array();
		foreach ($array as $k=>$v) {
			// Replace keys as well?
			$add_key = $k;
			if ($keys_too) {
				$add_key = str_replace($search, $replace, $k);
			}

			// Recurse
			$newArr[$add_key] = replaceTree($search, $replace, $v, $keys_too);
		}
		return $newArr;
	}